export default class Curl {
  #headers = {
    'Content-Type': 'application/json;charset=utf-8',
  };
  #dataType = 'json';
  constructor() {}
  setHeader(name = '', value = '') {
    if (Object.prototype.toString.call(name) === '[object Object]') {
      this.#headers = name;
    } else if (name && value && typeof name == 'string') {
      this.#headers[name] = value;
    }
    return this;
  }
  setDataType(value) {
    this.#dataType = value;
    return this;
  }
  get(url, data = {}) {
    return this.request(url, 'GET', data);
  }
  post(url, data = {}) {
    return this.request(url, 'POST', data);
  }
  //请求
  async request(url, method = 'GET', data = {}) {
    const rest = await uniCloud.httpclient.request(url, {
      method,
      headers: this.#headers,
      dataType: this.#dataType,
      data,
    });
    if (rest.status == 200) {
      return rest.data;
    }
    return rest;
  }
}
